package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.query.JSONResult;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.ProductQueryObject;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.UploadUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private ServletContext ctx;//网站的上下文

    //由于dataGrid组件返回必须为json,所以跳转界面就分开
    @RequestMapping("view")
    public String view() {
        return "product";
    }

    //页面需要的json数据
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(ProductQueryObject qo) {
        return productService.query(qo);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Product product, MultipartFile img) {
        JSONResult result = new JSONResult();
        try {
            if (img != null && img.getSize() > 0 && img.getContentType().startsWith("image/")) {
                //删除该对象旧的图片
                if (StringUtils.hasLength(product.getHeadImg())) {
                    UploadUtil.deleteFile(ctx, product.getHeadImg());
                }
                //把文件保存到服务器中
                String dir = ctx.getRealPath("/static/productImg");
                String imagePath = UploadUtil.upload(img, dir);
                //把图片服务器路径保存到对象中
                product.setHeadImg(imagePath);
            }
            productService.saveOrUpdate(product);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("失败");
        }
    }

    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            productService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @RequestMapping("selectByCount")
    @ResponseBody
    public Object selectByCount() {
        return productService.selectByCount();
    }
    @RequestMapping("selectByAvg")
    @ResponseBody
    public Object selectByAvg() {
        return productService.selectByAvg();
    }

    //导出
    @RequestMapping("exportProduct")
    public void exportProduct(HttpServletResponse response, ProductQueryObject qo) throws IOException {
        //设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=product.xls");
        //创建文件对象
        Workbook wb = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = wb.createSheet("exportProduct");
        //查询员工数据(要带高级查询的参数)
        List<Product> productList = productService.selectExportData(qo);
        //创建第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("商品名");
        /*row.createCell(1).setCellValue("图片");*/
        row.createCell(1).setCellValue("商品售价");
        row.createCell(2).setCellValue("一级分类");
        row.createCell(3).setCellValue("二级分类");
        row.createCell(4).setCellValue("商品编码");
        row.createCell(5).setCellValue("备注");
        //循环数据
        for (int i = 0; i < productList.size(); i++) {
            //从第二行开始创建行
            Row row1 = sheet.createRow(i + 1);
            Product product = productList.get(i);
            System.out.println(product);
            //创建单元格
            row1.createCell(0).setCellValue(product.getName());
            /*row1.createCell(1).setCellValue(product.getHeadImg());*/
            row1.createCell(1).setCellValue(product.getSalePrice().toString());
            row1.createCell(2).setCellValue(product.getFirst().getName());
            row1.createCell(3).setCellValue(product.getSecond().getClassify());
            row1.createCell(4).setCellValue(product.getSn());
            row1.createCell(5).setCellValue(product.getRemark());
        }

        //输出到单元格
        wb.write(response.getOutputStream());
    }
}
